/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

package com.huawei.housekeeper.service;

import com.huawei.saashousekeeper.config.TenantContext;

import lombok.extern.log4j.Log4j2;

import java.util.function.Function;

/**
 * 功能描述 servcie公用类
 *
 * @since 2022-03-10
 */
@Log4j2
public abstract class BaseService {

    /**
     * 更新一条记录成功返回的值
     */
    public static final int UPDATE_ONE_RECORD_SUCCESS = 1;

    /**
     * rabbitMq 调用方法增强
     *
     * @param tenantDomain
     * @param param
     * @param fun
     * @param <T> 函数接口入参类型
     * @param <R> 函数接口返回值类型
     */
    public static <T, R> R runWithTenantContext(String tenantDomain, T param, Function<T, R> fun) {
        TenantContext.set(tenantDomain);
        try {
            return fun.apply(param);
        } finally {
            TenantContext.remove();
        }
    }
}